<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>09_小黑的书架</title>
</head>

<body>
  <!-- 1. 渲染出数组数据 -->
  <!-- 2. 实现删除功能 -->
  <div id="app">
    <h3>小黑的书架</h3>
    <ul>
      <li v-for="(book, index) in booksList" :key="book.id">
        <span>{{book.name}}</span>
        <span>{{book.author}}</span>
        <!-- <button @click="del(book.id)">删除</button> -->
        <button @click="del(index)">删除</button>
      </li>
    </ul>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        booksList: [
          { id: 2324, name: '《红楼梦》', author: '曹雪芹' },
          { id: 2244, name: '《西游记》', author: '吴承恩' },
          { id: 3324, name: '《水浒传》', author: '施耐庵' },
          { id: 4645, name: '《三国演义》', author: '罗贯中' }
        ]
      },
      methods: {
        del(index) {
          // console.log('删除' , id);
          // 利用filter 筛选数组  id 跟传入的删除id不相等的留下来
          // filter(不改变原数组) 参数是个回调函数  函数有参数 一个参数就是当前项
          // this.booksList =this.booksList.filter(item => item.id !== id)
          this.booksList.splice(index, 1)
        }
      }
    })
  </script>
</body>

</html>